#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
'''
@File    :   server.py
@Time    :   2022/11/25 15:02:04
@Author  :   youle
@Email   :   a_fly0505@163.com
@Copyright : 侵权必究
'''

from flask import Flask, abort, jsonify, make_response, request, Response
from flask_cors import CORS
from core.config import Config
from stock.stock_hist import StockHist
from stock.query_stock import StockQuery
from technicalindicator.indicators import Specification
import json


# Flask跨域问题 第二种方法
# @app.after_request
# def func_res(resp):
#     res = make_response(resp)
#     res.headers['Access-Control-Allow-Origin'] = '*'
#     res.headers['Access-Control-Allow-Methods'] = 'GET,POST'
#     res.headers['Access-Control-Allow-Headers'] = 'x-requested-with,content-type'
#     return res

# print('form:', request.form)
# print('data:', request.data)
# print("args:", request.args)
# print("get_json:", request.get_json(force=True))

# app: Flask = None
app = Flask("afly_stock")
# r'/*' 是通配符，让本服务器所有的 URL 都允许跨域请求
CORS(app, resources=r'/*')


stock_hist: StockHist = None
stock_query: StockQuery = None

def Init() -> None:
    config = Config()
    stock_hist = StockHist(config)
    stock_query = StockQuery(config)


def Run() -> None:
    # app.run(host='0.0.0.0', port='8080', debug=True)
    app.run(host='0.0.0.0', port='8080')


@app.route('/login', methods=['post'])
def Login():
    print(request.data)
    print(request.json)


@app.route('/rsi', methods=['post'])
def GetRSI():
    print("content_type:", request.headers.get("content_type"))
    print("json:", request.json)
    # jsonData = json.loads(request.json)
    thr = request.json["threshold"]
    last = request.json["last"]
    timeperiod = request.json["timeperiod"]
    # self.stock_hist.GetRSI()
    codes = ["000333"]
    return jsonify({"code": codes})


@app.route('/stock', methods=['post'])
def GetStock():
    pass
